import 'package:flutter/material.dart';

import '../utils/my_data.dart';

///创建分组页面
class AddGroupApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return AddGroupState();
  }
}

class AddGroupState extends State {
  @override
  Widget build(BuildContext context) {
    TextEditingController controller = TextEditingController();
    GlobalKey key = GlobalKey<FormState>();
    return Scaffold(
      appBar: AppBar(
        title: Text("创建分组"),
      ),
      body: Column(
        children: [
          Form(
              key: key,
              child: Flex(
                direction: Axis.horizontal,
                children: [
                  Expanded(
                      flex: 1,
                      child: Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(left: 20),
                          child: Text("分组名称"))),
                  Expanded(
                      flex: 3,
                      child: Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(left: 20, right: 20),
                          child: TextFormField(
                            controller: controller,
                            validator: (v) {
                              return v == "" ? "名称不能为空" : null;
                            },
                          ))),
                ],
              )),
          Center(
            child: ElevatedButton(
                child: Text("保存"),
                onPressed: () async {
                  if ((key.currentState as FormState).validate()) {
                    //保存分组到数据库
                    await MyData.saveGroup(controller.text);
                    await MyData.getAllGroupAndAccount();
                    Navigator.pop(context, 1);
                  }
                }),
          )
        ],
      ),
    );
  }
}
